/**
 * Created by yu on 16/9/3.
 */

var http = require('http');
var url = require('url');
var fs = require('fs');
var queryString = require('querystring');
var util = require('util');
var formidable = require('formidable');


var server = http.createServer(function(req,res){
    var urlObj = url.parse(req.url,true);
    if(urlObj.pathname == '/'){
        res.setHeader("200",{'Content-type':'text/html;charset=utf-8'});
        fs.readFile('./index.html',function(err,data){
            res.write(data);
            res.end();
        })
    }else if(urlObj.pathname=='/post1'){
        //构建解析器
        var form = new formidable.IncomingForm();
        //接受客户端传过来的数据,将不通的input文本放进 fields里面,把上传的文件放在files中
        form.parse(req, function(err, fields, files) {
            console.log(fields, files);
            res.writeHead(200, {'content-type': 'text/plain'});
            res.write('received upload:\n\n');
            //inspect把对象转成字符串
            res.end(util.inspect({fields: fields, files: files}));
        })
    }
})

server.listen(8081,'localhost');

